/****************************************************************
 * <strong>MFDataRow</strong>
 *
 * <p>
 *FDataRow
 * </p>
 *
 * @author AndyZhang 2011 cnet2001@163.com
 * @version 0.1
 * ToDo:2010-11-29 ֧BigTable
 ***************************************************************/

package com.thinkwide.data.ormapping.datatable;

import com.thinkwide.data.core.MFString;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;


public class MFDataRow implements Iterable<Object> {
    private ArrayList fields;
    private MFDataRows dataRows;
    private MFDataState state;

    private String updateFields = "";

    public boolean hasUpdateFields() {
        return MFString.notNullOrEmpty(updateFields);
    }

    public void setUpdateFieldList(List<String> list) {
        updateFields = "";
        for (String item : list) {
            addUpdateField(item);
        }
    }

    public List<String> getUpdateFieldList() {
        String[] str = updateFields.split(",");
        List<String> list = new ArrayList<String>();
        for (String item : str) {
            list.add(item);
        }
        return list;
    }

    public void addUpdateField(String field) {
        if (MFString.isNullOrEmpty(updateFields))
            updateFields = field;
        else
            updateFields = updateFields + "," + field;
    }

    public MFDataRow() {
        this.fields = new ArrayList();
        state = MFDataState.none;
    }

    public MFDataRow(MFDataRows dataRows) {
        this();
        this.dataRows = dataRows;
    }

    private MFDataColumns getColumns() {
        return dataRows.getDataTable().getColumns();
    }

    public void setDataRows(MFDataRows dataRows) {
        this.dataRows = dataRows;
    }

    public MFDataRows getDataRows() {
        return dataRows;
    }

    public void setValue(int index, Object value) throws MFDataColumnException {
        MFDataColumn column = getColumns().getColumn(index);
        if (column != null) {
            while (fields.size() < index + 1)
                fields.add(null);

            fields.set(index, value);
        } else {
            throw new MFDataColumnException("Out of index!");
        }

    }


    public void setValue(String fieldName, Object value) throws MFDataColumnException {
        MFDataColumn column = getColumns().getColumn(fieldName);
        if (column != null) {
            setValue(column.getIndex(), value);
        } else {
            throw new MFDataColumnException("Not Found This FieldName:" + fieldName);
        }

    }

    public Object getValue(int index) {
        if (fields.size() > index)
            return fields.get(index);
        else
            return null;
    }

    public Object getValue(String fieldName) throws MFDataColumnException {
        MFDataColumn column = getColumns().getColumn(fieldName);
        if (column != null) {
            return getValue(column.getIndex());
        } else {
            throw new MFDataColumnException("Not Found This FieldName:" + fieldName);
        }

    }

    public String getString(int index) {
        return MFDTConvert.toString(getValue(index));
    }

    public int getInt(int index) throws MFDataColumnException {
        return MFDTConvert.toInt(getValue(index));
    }

    public float getFloat(int index) throws MFDataColumnException {
        return MFDTConvert.toFloat(getValue(index));
    }

    public Date getDate(int index) throws MFDataColumnException {
        return MFDTConvert.toDate(getValue(index));
    }


    public double getDouble(int index) throws MFDataColumnException {
        return MFDTConvert.toDouble(getValue(index));
    }

    public long getLong(int index) throws MFDataColumnException {
        return MFDTConvert.toLong(getValue(index));
    }

    public BigDecimal getBigDecimal(int index) throws MFDataColumnException {
        return MFDTConvert.toBigDecimal(getValue(index));
    }

    public String getString(String fieldName) throws MFDataColumnException {
        return MFDTConvert.toString(getValue(fieldName));
    }

    public int getInt(String fieldName) throws MFDataColumnException {
        return MFDTConvert.toInt(getValue(fieldName));
    }

    public double getDouble(String fieldName) throws MFDataColumnException {
        return MFDTConvert.toDouble(getValue(fieldName));
    }

    public long getLong(String fieldName) throws MFDataColumnException {
        return MFDTConvert.toLong(getValue(fieldName));
    }

    public BigDecimal getBigDecimal(String fieldName) throws MFDataColumnException {
        return MFDTConvert.toBigDecimal(getValue(fieldName));
    }

    public float getFloat(String fieldName) throws MFDataColumnException {
        return MFDTConvert.toFloat(getValue(fieldName));
    }

    public Date getDate(String fieldName) throws MFDataColumnException {
        return MFDTConvert.toDate(getValue(fieldName));
    }

    public void clear() {
        this.fields.clear();
    }


    public Iterator<Object> iterator() {
        Iterator<Object> it =
                new Iterator<Object>() {
                    private int index = 0;

                    public boolean hasNext() {

                        return index < fields.size();
                    }

                    public Object next() {
                        return fields.get(index++);
                    }

                    public void remove() {
                        throw new UnsupportedOperationException();
                    }
                };
        return it;
    }

    public void setState(MFDataState state) {
        this.state = state;
    }

    public MFDataState getState() {
        return state;
    }

    public void setModifyState() {
        this.state = MFDataState.modify;
    }

    public void setInsertState() {
        this.state = MFDataState.insert;
    }

    public void setDeleteState() {
        this.state = MFDataState.delete;
    }

    public void setNoneState() {
        this.state = MFDataState.none;
    }

    public void setUpdateFields(String updateFields) {
        this.updateFields = updateFields;
    }

    public String getUpdateFields() {
        return updateFields;
    }
}
